package com.link.base.base.questionnaire.service;

import com.link.base.base.questionnaire.dao.mybatis.mapper.QuesAnsMapper;
import com.link.base.base.questionnaire.model.QuesAns;
import com.link.base.base.questionnaire.model.QuesAnsDetail;
import com.link.base.base.questionnaire.model.QuesAnsItem;
import com.link.core.basic.dao.mapper.BasicMapper;
import com.link.core.basic.service.BasicServiceImpl;
import com.link.core.basic.service.ServiceException;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author unknown
 * @date unknown
 * @comments 问卷
 */
@Service
public class QuesAnsServiceImpl extends BasicServiceImpl<QuesAns> implements QuesAnsService {

    @Resource
    private QuesAnsMapper quesAnsMapper;
    @Resource
    private QuesAnsItemService quesAnsItemService;
    @Resource
    private QuesAnsDetailService quesAnsDetailService;

    @Override
    public BasicMapper<QuesAns> getBasicMapper() {
        return quesAnsMapper;
    }

    @Override
    public QuesAns getQuesAnsById(QuesAns entity) throws Exception {
        if (null == entity.getEmpId()) {
            throw new ServiceException("PUBLIC-016");
        }
        if (null == entity.getQuestionId()) {
            throw new ServiceException("QUESTION-001");
        }

        QuesAns quesAnsParam = new QuesAns();
        quesAnsParam.setEmpId(entity.getEmpId());
        quesAnsParam.setQuestionId(entity.getQuestionId());
        quesAnsParam.setPageFlag(false);
        List<QuesAns> quesAnsList = queryByExamplePage(quesAnsParam);
        if (quesAnsList.size() != 1) {
            throw new ServiceException("QUESTION-002");
        }
        QuesAns quesAns = quesAnsList.get(0);

        //问卷答案
        QuesAnsItem quesAnsItemParam = new QuesAnsItem();
        quesAnsItemParam.setAnswerId(quesAns.getId());
        quesAnsItemParam.setPageFlag(false);
        List<QuesAnsItem> ansItemList = quesAnsItemService.queryByExamplePage(quesAnsItemParam);

        for (QuesAnsItem item : ansItemList) {
            QuesAnsDetail quesAnsDetailParam = new QuesAnsDetail();
            quesAnsDetailParam.setAnswerQuestionItemId(item.getId());
            quesAnsDetailParam.setPageFlag(false);
            List<QuesAnsDetail> ansDetailList = quesAnsDetailService.queryByExamplePage(quesAnsDetailParam);
            item.setAnsDetailList(ansDetailList);
        }
        quesAns.setAnsItemList(ansItemList);
        return quesAns;
    }

    @Override
    public QuesAns getQuesAnsByQuesIdAndUserId(QuesAns entity) throws Exception {
        if (null == entity.getEmpId()) {
            throw new ServiceException("PUBLIC-016");
        }
        if (null == entity.getQuestionId()) {
            throw new ServiceException("QUESTION-001");
        }

        QuesAns quesAnsParam = new QuesAns();
        quesAnsParam.setEmpId(entity.getEmpId());
        quesAnsParam.setQuestionId(entity.getQuestionId());
        quesAnsParam.setPageFlag(false);
        List<QuesAns> quesAnsList = queryByExamplePage(quesAnsParam);
        if (quesAnsList.size() >= 1) {
            QuesAns quesAns = quesAnsList.get(0);

            //问卷答案
            QuesAnsItem quesAnsItemParam = new QuesAnsItem();
            quesAnsItemParam.setAnswerId(quesAns.getId());
            quesAnsItemParam.setPageFlag(false);
            List<QuesAnsItem> ansItemList = quesAnsItemService.queryByExamplePage(quesAnsItemParam);

            for (QuesAnsItem item : ansItemList) {
                QuesAnsDetail quesAnsDetailParam = new QuesAnsDetail();
                quesAnsDetailParam.setAnswerQuestionItemId(item.getId());
                quesAnsDetailParam.setPageFlag(false);
                List<QuesAnsDetail> ansDetailList = quesAnsDetailService.queryByExamplePage(quesAnsDetailParam);
                item.setAnsDetailList(ansDetailList);
            }
            quesAns.setAnsItemList(ansItemList);
            return quesAns;
        } else {
            return new QuesAns();
        }

    }
}
